package com.wn.controller;

import cn.hutool.jwt.JWTUtil;
import com.wn.common.Result;
import com.wn.model.dto.UserDTO;
import com.wn.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author yellow Docter
 * @date 2023 -06-01
 * @desc
 */
@Api(tags = "用户管理模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户模块的测试接口")
    @GetMapping("/hello")
    public  String hello(){
        return "hello";
    }

    /**
     * 请求参数：json
     * 请求方式：post
     * 请求路径：/login
     * 返回结果：统一返回结果
     */
    @ApiOperation("用户登录操作")
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO user){

        //业务逻辑实现
        boolean flag = userService.findUserById(user.getUsername(), user.getPassword());

        //登录成功
        if (flag){
            Map<String,Object> playLoad = new HashMap<>();
            playLoad.put("username",user.getUsername());
            String token = JWTUtil.createToken(playLoad, "1234".getBytes());
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            return Result.ok(map);
        }

        return Result.fail("登录失败...");
    }
}
